package com.walterweimod.auxiliary_attributes.capability.impl;

import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.Objects;

public class RageLayerValue extends ChargeLivingEntityAttributeRangedValueBase<Integer>
        implements INBTSerializable<CompoundTag> {
    private static final String DESCRIPTION = "rage_layer_value";

    @Override
    public void updateMinMaxDefaultValue(){
        if (Objects.nonNull(getLivingEntity())){
            AttributeInstance max = getLivingEntity().getAttribute(getAttributeMap().get("max"));
            AttributeInstance min = getLivingEntity().getAttribute(getAttributeMap().get("min"));

            if (Objects.nonNull(max)){
                setMaxValue(
                        (int) ((float) max.getValue())
                );
            }

            if (Objects.nonNull(min)){
                setMinValue(
                        (int) ((float) min.getValue())
                );
            }

            setDefaultValue(0);
        }
    }

    @Override
    public void addValue(Integer value) {
        setValue(getValue() + value);
    }

    @Override
    public void clearValue() {
        setValue(0);
        setMinValue(0);
        setMaxValue(0);
    }

    public static String getDescription(){
        return DESCRIPTION;
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag = new CompoundTag();
        tag.putInt(DESCRIPTION, Objects.nonNull(getValue()) ? getValue() : 0);
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        setValue(nbt.getInt(DESCRIPTION));
    }
}
